<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;

/**
 * App\Models\Card
 *
 * @property int $id
 * @property string $title 标题
 * @property float $price 购买价格
 * @property int $number 洗车次数
 * @property int $days 洗车有效天数
 * @property int $range 使用限制:0=全场通用,1=指定服务
 * @property int $shop_id 门店ID
 * @property int $cid 分类ID
 * @property int $service_id 服务ID
 * @property array $services 服务信息
 * @property string|null $areas 所属地区ID组,默认null表示全国地区显示
 * @property array|null $rates 分成比例
 * @property int $status 状态:0=正常,1=隐藏
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|Card newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Card newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Card query()
 * @method static \Illuminate\Database\Eloquent\Builder|Card whereAreas($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Card whereCid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Card whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Card whereDays($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Card whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Card whereNumber($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Card wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Card whereRange($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Card whereServiceId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Card whereShopId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Card whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Card whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Card whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Card extends Model
{
	use HasDateTimeFormatter;

    protected $casts = ['price'=>'float','rates'=>'json','services'=>'json'];

    public static function getRanges(): array
    {
        return [0=>'店铺通用',1=>'指定服务'];
    }


    protected static function booted()
    {
        self::saving(function (Card $card){
            if (!$card->range){
                $card->services = null;
            }else{
                $card->number = 0;
            }
        });
        self::creating(function (Card $card){
            if ($card->range){
                $services = $card->services;
                if ($services){
                    foreach ($services as $key=>$service){
                        $item = Service::find($service['s_id']);
                        $services[$key]['title'] = $item->title;
                    }
                    $card->services = $services;
                }
            }
        });
        self::updating(function (Card $card){
            if ($card->range){
                $services = $card->services;
                if ($services){
                    foreach ($services as $key=>$service){
                        $item = Service::find($service['s_id']);
                        $services[$key]['title'] = $item->title;
                    }
                    $card->services = $services;
                }
            }
        });
    }

    public static function getList(): array
    {
        $shop_id = request()->input("shop_id","");
        $pageSize = request()->input("pageSize",10);
        $list = self::query()
            ->where("status",0)
            ->when($shop_id,function ($query, $value){
                $query->where("shop_id",$value);
            })
            ->orderByDesc("id")
            ->paginate($pageSize);
        $data['items'] = $list->items();
        $data['lastPage'] = $list->lastPage();
        $data['total'] = $list->total();
        return $data;
    }

    public static function getDetail($id, $user_id): array
    {
        $card = self::query()->where("id",$id)->first();
        if ($card){

        }
        $data['item'] = $card;
        return $data;
    }
}
